package com.nativetalk.service.three.course;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentCouse;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.course.TsNtCourseInfoIndexTwo;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.setmeal.TsNtSetmealIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtBannerInfoDao;
import com.nativetalk.dao.TsNtCourseInfoDao;
import com.nativetalk.dto.course.CourseCategoryDto;
import com.nativetalk.dto.course.CourseDto;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by lizhun on 2016/9/26.
 */
@Service
public class CourseServiceImpl implements CourseService {
    private static Logger logger = Logger.getLogger(CourseServiceImpl.class);
    @Resource
    private TdNtBannerInfoDao tdNtBannerInfoDao;

    @Resource
    private TsNtCourseInfoDao courseInfoDao;

    @Override
    public RetInfo index(String app_type) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            Map<String, Object> map = new HashMap<>();
            //  查询banner图
            map.put("banner", tdNtBannerInfoDao.findTdNtBannerInfoIndexByType(app_type,2));

            // 查询课程信息
            List<CourseCategoryDto> courseList = courseInfoDao.findCourseListWithCategory(app_type);
            map.put("course", courseList);

            //  查询视频介绍图
            map.put("video", tdNtBannerInfoDao.findTdNtBannerInfoIndexByType(app_type,3));

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo list(String cate_id) {
        String logInfo = this.getClass().getName() + ":list:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String, Object> map = new HashMap<>();
        try {
            // 查询课程信息
            List<CourseDto> courseList = courseInfoDao.findCourseListByCateId(cate_id);
            map.put("course", courseList);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error("==========error line:" + e.getStackTrace()[0].getLineNumber());
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo getlist(String cate_id) {
        String logInfo = this.getClass().getName() + ":list:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map = new HashMap<>();

            Map<String, List<TsNtCourseInfoIndexTwo>> courseMapIdTwo = (Map<String, List<TsNtCourseInfoIndexTwo>>) MemcachedUtils.get(MemcachedKey.trialCourseMapIdTwo);
            List<TsNtCourseInfoIndexTwo> tsNtCourseInfoIndexTwos = courseMapIdTwo.get(cate_id);//课程二级分类列表

            map.put("course", tsNtCourseInfoIndexTwos);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error("==========error line:" + e.getStackTrace()[0].getLineNumber());
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public List<TdNtCoursePictureList> findCoursePicture(String str_cou_id) {
        Integer cou_id = Integer.parseInt(str_cou_id);

        List<TdNtCoursePictureList> list = courseInfoDao.findCoursePicture(cou_id);
        return list;

    }

    public TsNtCourseInfoDetail findByCouId(Integer cou_id){
        return courseInfoDao.findCourseByCouId(cou_id);
    }

    public List<TdNtCommentCouse> findCommentByCouId(String cou_id){
        return courseInfoDao.findCommentByCouId(cou_id);
    }

}
